package com.module.app.utils;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.module.base.utils.CommonUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.module.app.R;
import com.module.frame.glide.GlideApp;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * @author bsnl_yanxuwen
 */
public class GlideUtils {
    public static void load(Context context, ImageView iv, Object url) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error(R.drawable.app_default_logo)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .placeholder(R.drawable.app_default_logo)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    public static void load(Context context, ImageView iv, Object url, @DrawableRes int error) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error(error)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    /**
     * 不执行skipMemoryCache
     */
    public static void load2(Context context, ImageView iv, Object url) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error(R.drawable.app_default_logo)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.app_default_logo)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    public static void loadNet(Context context, ImageView iv, String url) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error((!TextUtils.isEmpty(url) && CommonUtils.isNetwork()) ? R.mipmap.app_bg_illegal : R.drawable.app_default_logo)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.app_default_logo)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    public static void loadNet(Context context, ImageView iv, String url, @DrawableRes int error) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error((!TextUtils.isEmpty(url) && CommonUtils.isNetwork()) ? R.mipmap.app_bg_illegal : error)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(error)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    /**
     * 加载背景主题
     */
    public static void loadBg(Context context, ImageView iv, String url) {
        GlideApp.with(context)
                .load(url)
                .error((!TextUtils.isEmpty(url) && CommonUtils.isNetwork()) ? R.mipmap.app_bg_illegal : R.drawable.app_default_logo)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                .into(iv);
    }

    public static void loadNet(Context context, ImageView iv, String url, @DrawableRes int placeholder, @DrawableRes int error) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context)
            .load(url)
            .error(error)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .placeholder(placeholder)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    public static void loadNet(Context context, ImageView iv, String url, boolean placeholder) {
        //placeholder 会影响清晰度
        if (placeholder) {
            loadNet(context, iv, url);
        } else {
            //DrawableCrossFadeFactory  解决png还会显示占位图问题
            GlideApp.with(context)
                .load(url)
                .error(R.drawable.app_default_logo)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                .into(iv);
        }

    }

    /**
     * @param placeholder 是否使用展位图
     */
    public static void load(Context context, ImageView iv, Object url, boolean placeholder) {
        if (url != null) {
            if (placeholder) {
                //DrawableCrossFadeFactory  解决png还会显示占位图问题
                GlideApp.with(context)
                    .load(url)
                    .error(R.drawable.app_default_logo)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .placeholder(R.drawable.app_default_logo)
                    .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                    .into(iv);
            } else {
                //DrawableCrossFadeFactory  解决png还会显示占位图问题
                GlideApp.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                    .into(iv);
            }

        }
    }

    /**
     * @param placeholder 是否使用展位图
     */
    public static void load(Context context, ImageView iv, Object url, boolean placeholder, boolean skipMemoryCache) {
        if (url != null) {
            if (placeholder) {
                //DrawableCrossFadeFactory  解决png还会显示占位图问题
                GlideApp.with(context)
                    .load(url)
                    .error(R.drawable.app_default_logo)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(skipMemoryCache)
                    .placeholder(R.drawable.app_default_logo)
                    .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                    .into(iv);
            } else {
                //DrawableCrossFadeFactory  解决png还会显示占位图问题
                GlideApp.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(skipMemoryCache)
                    .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build()))
                    .into(iv);
            }

        }
    }

    public static void loadOverride(Context context, ImageView iv, Object url, boolean skipMemoryCache, int width, int height) {
        GlideApp.with(context)
            .load(url)
            .error(R.drawable.app_default_logo)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(skipMemoryCache)
            .placeholder(R.drawable.app_default_logo)
            .override(width, height)
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    public static void loadBlur(Context context, ImageView iv, Object url) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context).load(url)
            .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 8)))
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build()))
            .into(iv);
    }

    /**
     * @param radius 最大25
     */
    public static void loadBlur(Context context, ImageView iv, Object url, int radius, int sampling) {
        //DrawableCrossFadeFactory  解决png还会显示占位图问题
        GlideApp.with(context).load(url)
            .apply(RequestOptions.bitmapTransform(new BlurTransformation(radius, sampling)))
            .transition(DrawableTransitionOptions.with(new DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build()))
            .into(iv);
    }


    /**
     * 清除图片磁盘缓存
     */
    public void clearImageDiskCache(final Context context) {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(context).clearDiskCache();
// BusUtil.getBus().post(new GlideCacheClearSuccessEvent());
                    }
                }).start();
            } else {
                Glide.get(context).clearDiskCache();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除图片内存缓存
     */
    public void clearImageMemoryCache(Context context) {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行
                Glide.get(context).clearMemory();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除图片所有缓存
     */
    public void clearImageAllCache(Context context) {
        clearImageDiskCache(context);
        clearImageMemoryCache(context);
    }
}
